package main

import (
	"log"

	"github.com/micro/go-micro"
	handler "git.oschina.net/fanbuchi/xgggh/oauth2/handler"
	"git.oschina.net/fanbuchi/xgggh/oauth2/subscriber"
	oauth2 "git.oschina.net/fanbuchi/xgggh/oauth2/proto/oauth2"
	account "git.oschina.net/fanbuchi/xgggh/oauth2/proto/account"
	client "git.oschina.net/fanbuchi/xgggh/oauth2/proto/client"
	role "git.oschina.net/fanbuchi/xgggh/oauth2/proto/role"
	scope "git.oschina.net/fanbuchi/xgggh/oauth2/proto/scope"
	_ "git.oschina.net/fanbuchi/xgggh/oauth2/db"

)

func main() {
	// New Service
	service := micro.NewService(
		micro.Name("com.xgggh.srv.oauth2"),
		micro.Version("latest"),
	)

	// Register Handler
	scope.RegisterScopeHandler(service.Server(), new(handler.Scope))
	role.RegisterRoleHandler(service.Server(), new(handler.Role))
	client.RegisterClientHandler(service.Server(), new(handler.Client))
	account.RegisterAccountHandler(service.Server(), new(handler.Account))
	oauth2.RegisterOauth2Handler(service.Server(), new(handler.Oauth2))

	// Register Struct as Subscriber
	service.Server().Subscribe(
		service.Server().NewSubscriber("topic.com.xgggh.srv.oauth2", new(subscriber.Account)),
	)

	// Register Function as Subscriber
	service.Server().Subscribe(
		service.Server().NewSubscriber("topic.com.xgggh.srv.oauth2", subscriber.Handler),
	)

	// Initialise service
	service.Init()

	// Run service
	if err := service.Run(); err != nil {
		log.Fatal(err)
	}
}
